package com.yanqu.road.server.manager.activity.zhenhuan;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.activity.zhenhuan.eZhenHuanChildType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.activity.zhenhuan.ZhenHuanFirstProcessConfig;
import com.yanqu.road.logic.activity.zhenhuan.ZhenHuanSecondProcessConfig;
import com.yanqu.road.logic.activity.zhenhuan.ZhenHuanThirdProcessConfig;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CrossZhenHuanConfigMgr extends TempCrossMgr {
    private static Map<Integer, ZhenHuanFirstProcessConfig> firstProcessConfigMap = new HashMap<>();
    private static Map<Integer, ZhenHuanSecondProcessConfig> secondProcessConfigMap = new HashMap<>();

    private static Map<Integer, ZhenHuanThirdProcessConfig> thirdProcessConfigMap = new HashMap<>();

    @Override
    public boolean removeExpireData() throws Exception {
        long currentSecond = DateHelper.getCurrentTime() / 1000;
        List<Integer> needRemoveIdList = new ArrayList<>();

        for (ZhenHuanFirstProcessConfig config : firstProcessConfigMap.values()) {
            ActivityInfo activityInfo = config.getActivityInfo();
            if (currentSecond > activityInfo.getEndShowTime() + 1 * DateHelper.HOUR_SECONDS) {
                needRemoveIdList.add(activityInfo.getActivityId());
            }
        }

        for (Integer activityId : needRemoveIdList) {
            firstProcessConfigMap.remove(activityId);
        }
        needRemoveIdList.clear();

        for (ZhenHuanSecondProcessConfig config : secondProcessConfigMap.values()) {
            ActivityInfo activityInfo = config.getActivityInfo();
            if (currentSecond > activityInfo.getEndShowTime() + 1 * DateHelper.HOUR_SECONDS) {
                needRemoveIdList.add(activityInfo.getActivityId());
            }
        }

        for (Integer activityId : needRemoveIdList) {
            secondProcessConfigMap.remove(activityId);
        }
        needRemoveIdList.clear();

        for (ZhenHuanThirdProcessConfig config : thirdProcessConfigMap.values()) {
            ActivityInfo activityInfo = config.getActivityInfo();
            if (currentSecond > activityInfo.getEndShowTime() + 1 * DateHelper.HOUR_SECONDS) {
                needRemoveIdList.add(activityInfo.getActivityId());
            }
        }

        for (Integer activityId : needRemoveIdList) {
            thirdProcessConfigMap.remove(activityId);
        }

        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        reloadActivityConfig();
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static void reloadActivityConfig() {
        getLogger().info("reload CrossZhenHuanConfigMgr start");
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.ZhenHuan.getValue());

        Map<Integer, ZhenHuanFirstProcessConfig> tempFirstProcessConfigMap = new HashMap<>();
        Map<Integer, ZhenHuanSecondProcessConfig> tempSecondProcessConfigMap = new HashMap<>();
        Map<Integer, ZhenHuanThirdProcessConfig> tempThirdProcessConfigMap = new HashMap<>();
        List<Integer> activityIdList = new ArrayList<>();
        Map<Integer, ActivityInfo> activityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMapByCrossServerId(activityTypeList, nowTimeStr, CrossBaseServer.getInstance().getServerId());
        for (ActivityInfo item : activityInfoMap.values()) {
            activityIdList.add(item.getActivityId());
        }

        Map<Integer, Map<String, ActivityConfig>> allActivityConfigMap = ActivityBussiness.getActivityConfigMap(activityIdList);

        for (ActivityInfo activityInfo : activityInfoMap.values()) {
            Map<String, ActivityConfig> activityConfigMap = allActivityConfigMap.get(activityInfo.getActivityId());
            if (activityConfigMap == null) {
                continue;
            }
            if (activityInfo.getChildType() == eZhenHuanChildType.First.getValue()) {
                ZhenHuanFirstProcessConfig config = new ZhenHuanFirstProcessConfig(activityInfo, activityConfigMap);
                tempFirstProcessConfigMap.put(activityInfo.getActivityId(), config);
            } else if (activityInfo.getChildType() == eZhenHuanChildType.Second.getValue()) {
                ZhenHuanSecondProcessConfig config = new ZhenHuanSecondProcessConfig(activityInfo, activityConfigMap);
                tempSecondProcessConfigMap.put(activityInfo.getActivityId(), config);
            } else if (activityInfo.getChildType() == eZhenHuanChildType.Third.getValue()) {
                ZhenHuanThirdProcessConfig config = new ZhenHuanThirdProcessConfig(activityInfo, activityConfigMap);
                tempThirdProcessConfigMap.put(activityInfo.getActivityId(), config);
            }
        }

        firstProcessConfigMap = tempFirstProcessConfigMap;
        secondProcessConfigMap = tempSecondProcessConfigMap;
        thirdProcessConfigMap = tempThirdProcessConfigMap;
        getLogger().info("reload CrossZhenHuanConfigMgr end");
    }

    public static void reloadZhenHuanConfig(ActivityInfo activityInfo) {
        List<Integer> activityIdList = new ArrayList<>();
        int activityId = activityInfo.getActivityId();
        activityIdList.add(activityId);
        Map<String, ActivityConfig> activityConfigMap = ActivityBussiness.getActivityConfigMap(activityIdList).get(activityId);

        if (activityInfo.getChildType() == eZhenHuanChildType.First.getValue()) {
            ZhenHuanFirstProcessConfig config = new ZhenHuanFirstProcessConfig(activityInfo, activityConfigMap);
            firstProcessConfigMap.put(activityInfo.getActivityId(), config);
        } else if (activityInfo.getChildType() == eZhenHuanChildType.Second.getValue()) {
            ZhenHuanSecondProcessConfig config = new ZhenHuanSecondProcessConfig(activityInfo, activityConfigMap);
            secondProcessConfigMap.put(activityInfo.getActivityId(), config);
        } else if (activityInfo.getChildType() == eZhenHuanChildType.Third.getValue()) {
            ZhenHuanThirdProcessConfig config = new ZhenHuanThirdProcessConfig(activityInfo, activityConfigMap);
            thirdProcessConfigMap.put(activityInfo.getActivityId(), config);
        }
    }

    public static ZhenHuanFirstProcessConfig getFirstProcessConfig(int activityId) {
        return firstProcessConfigMap.get(activityId);
    }

    public static ZhenHuanSecondProcessConfig getSecondProcessConfig(int activityId) {
        return secondProcessConfigMap.get(activityId);
    }

    public static ZhenHuanThirdProcessConfig getThirdProcessConfig(int activityId) {
        return thirdProcessConfigMap.get(activityId);
    }

    public static int getPromotionNum(int activityId) {
        if (firstProcessConfigMap.containsKey(activityId)) {
            return firstProcessConfigMap.get(activityId).getPromotionNum();
        }

        if (secondProcessConfigMap.containsKey(activityId)) {
            return secondProcessConfigMap.get(activityId).getPromotionNum();
        }

        return 0;
    }

}
